package com.chinasolf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinasoft.model.hosp.HospitalSet;
import com.chinasoft.vo.hosp.HospitalSetQueryVo;
import com.chinasolf.result.R;
import com.chinasolf.service.HospitalSetService;
import com.chinasolf.utils.HttpRequestHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

//医院设置接口
@Api(description = "医院设置接口")

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //查询所有医院设置
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation(value = "医院设置删除")
    @GetMapping("remove/{id}")
    public R removeHospSet(@ApiParam(name = "id", value = "ID", required = true) @PathVariable Long id) {
        boolean isSuccess = hospitalSetService.removeById(id);
        if(isSuccess) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "分页医院设置列表")
    @GetMapping("findPage/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<HospitalSet> page1 = new Page<>(page, limit);
        hospitalSetService.page(page1, null);
        List<HospitalSet> records = page1.getRecords();
        long total = page1.getTotal();
        return R.ok().data("total", total).data("records", records);
    }

    //4 条件查询带分页
    @PostMapping("findQuery/{current}/{limit}")
    public R findQuery(@PathVariable long current,
                       @PathVariable long limit,
                       @RequestBody(required = false)  HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象
        Page<HospitalSet> pageParam = new Page<>(current,limit);
        if(hospitalSetQueryVo == null) {
            //调用方法
            hospitalSetService.page(pageParam);
            //返回查询结果
            long total = pageParam.getTotal();
            List<HospitalSet> list = pageParam.getRecords();
            return R.ok().data("total",total).data("rows",list);
        } else {
            //获取条件值
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();
            //封装条件
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            //判断条件值是否为空
            if(!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname",hosname);
            }
            if(!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode",hoscode);
            }
            //调用方法
            hospitalSetService.page(pageParam,wrapper);
            //返回查询结果
            long total = pageParam.getTotal();
            List<HospitalSet> list = pageParam.getRecords();
            return R.ok().data("total",total).data("rows",list);
        }
    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("saveHospSet")
    public R saveHospSet(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {
        String signKey = System.currentTimeMillis()+""+new Random().nextInt(1000);
        hospitalSet.setSignKey(signKey);
        boolean isSuccess = hospitalSetService.save(hospitalSet);
        if(isSuccess){
            Map<String,Object> map = new HashMap<>();
            map.put("sign",signKey);
            map.put("hoscode",hospitalSet.getHoscode());
            HttpRequestHelper.sendRequest(map,"http://localhost:9998/hospSet/updateSignKey");
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("findHospSet/{id}")
    public R findHospSet(
            @ApiParam(name = "id", value = "医院设置id", required = true)
            @PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet", hospitalSet);

    }

    @ApiOperation("修改医院设置")
    @PostMapping("updateHospSet")
    public R updateHospSet(@ApiParam(name = "hospitalSet", value = "修改值") @RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("batchRemove")
    public R batchRemove(
            @ApiParam(name="idList",value = "批量删除id")
            @RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }
    @ApiOperation("设置医院锁定和解锁")
    @GetMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@ApiParam(name = "id",value = "设置医院id") @PathVariable Long id,
                             @ApiParam(name = "status",value = "设置是否锁定")@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }


}